﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using System.Web.Configuration;

namespace CantinhodaCegonha.Models.Helpers
{
    class DateTimeModelBinder : IModelBinder
    {

        #region IModelBinder Members

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {

            var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            var date = value.ConvertTo(typeof(DateTime), CultureInfo.GetCultureInfo(WebConfigurationManager.AppSettings["Culture"]));

            return date;

        }

        #endregion

    }

    class NullDateTimeModelBinder : IModelBinder
    {

        #region IModelBinder Members

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {

            var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            if (value == null) return null;
            var date = value.ConvertTo(typeof(DateTime?), CultureInfo.GetCultureInfo(WebConfigurationManager.AppSettings["Culture"]));

            return date;

        }

        #endregion

    }

}